﻿package de.gameduell.framework.facebook.app.pages {
	import de.gameduell.framework.facebook.app.Controller;
	import de.gameduell.framework.facebook.app.GameData;
	import de.gameduell.framework.facebook.app.PageManager;
	import de.gameduell.framework.facebook.app.pyramide.ForeignPyramidView;
	import de.gameduell.framework.facebook.app.pyramide.Pyramid;

	import de.gameduell.framework.debug.Debug;
	import de.gameduell.framework.facebook.Page;
	import de.gameduell.framework.facebook.event.GameStateEvent;
	import de.gameduell.framework.gui.button.Button;
	import de.gameduell.framework.gui.button.ButtonEvent;

	import flash.display.Sprite;
	import flash.events.Event;
	import flash.system.System;

	public class ForeignPyramidPage extends Page {
		private var pageManager:PageManager;
		private var backButton:Button;
		private var pyramid:Pyramid;
		private var pyramidView:ForeignPyramidView;
		private var currentUserId:String;

		public function ForeignPyramidPage(aPageManager:PageManager, anId:String) {
			super(aPageManager, anId);
			pageManager = aPageManager;
		}

		override protected function initPage():void {
			backButton = new Button(page.getChildByName("backButton") as Sprite, "backToMyPyramid");
			backButton.toolTip = true;
			backButton.addEventListener(ButtonEvent.CLICK, showStartPage);
			controller.communication.addEventListener(GameStateEvent.FOREIGN_GAMESTATE_RECEIVED, onGameState);
		}

		private function onGameState(event:GameStateEvent):void {
			if(event.gameState.key == GameData.KEY_PYRAMID) {
				//				Debug.traceHTML("ForeignPyramidPage::onGameState: " + event.gameState.key + ":" + event.gameState.value, Debug.ERROR);
				if(pyramid == null) {
					pyramid = new Pyramid(true);
					pyramid.setup(Controller(controller).xmlMissionList, Controller(controller).brickConditionList);
				}
				pyramid.stringToPyramid(event.gameState.value, true); // do a cleanup before ...
				if(pyramidView == null) {
					pyramidView = new ForeignPyramidView(page, pyramid);
				} else {
					pyramidView.update();
				}
			}
		}

		private function showStartPage(event:Event):void {
			pageManager.show(PageManager.START_PAGE);
			// in case it's enabled
			if(controller.featureList.isEnabled(Controller.JACKPOT_FEATURE_ID)) {
				pageManager.show(PageManager.JACKPOT);
			}
		}

		override public function show(event:Event = null):void {
			if(page != null) {
				if(!active) {
					sprite.addChild(page);
					active = true;
				}
				page.alpha = 1;
			} else {
				controller.loadAsset(_id);
			}
			updatePyramidUser();
		}

		public function updatePyramidUser():void {
			if(page != null) {
				var uid:String = Controller(controller).foreignUserId;
				localeData.setValue("pyramidOwner", controller.facebookData.getFriendProfileByID(uid).firstName);
				if(currentUserId != uid) {
					currentUserId = uid;
					controller.communication.requestFetchGamestateByKey(GameData.KEY_PYRAMID, uid);
				}
			}
			Debug.trace("ForeignPyramidPage::show: MEMORY USAGE: " + System.totalMemory, Debug.WARNING);
		}

		override public function hide(event:Event = null):void {
			super.hide();
			currentUserId = null;
		}
	}
}